/**
 * Copyright (c) 2005-2011 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: Fixtures.java 1593 2011-05-11 10:37:12Z calvinxiu $
 */
package net.diaowen.common.utils;

import net.diaowen.dwsurvey.entity.Prize;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *   
 */
public abstract class SystemUtils {





	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
//序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {
		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
            //反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {
		}
		return null;
	}

	// 读取cell单元格的值，不对日期格式进行处理
	@SuppressWarnings("deprecation")
	public static String getCellValue(HSSFCell cell) {
		String returnValue;
		if (cell == null)
			return "";
		if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
			return cell.getStringCellValue();
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {

			return String.valueOf(cell.getBooleanCellValue());
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
			//强制转换成字符串格再读取
			cell.setCellType(XSSFCell.CELL_TYPE_STRING);
			//再读取
			returnValue= cell.getStringCellValue();
			return returnValue;
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            //强制转换成字符串格再读取
			cell.setCellType(XSSFCell.CELL_TYPE_STRING);
			//再读取
		    returnValue= cell.getStringCellValue();
			return returnValue;
		}
		return "";
	}
	
	// 读取cell单元格的值,若为日期格式，进行转换
	@SuppressWarnings("deprecation")
	public static String getCellValueDate(HSSFCell cell) {
		String returnValue;
		if (cell == null)
			return "";
		if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
			return cell.getStringCellValue();
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {

			return String.valueOf(cell.getBooleanCellValue());
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
			//强制转换成字符串格再读取
			cell.setCellType(XSSFCell.CELL_TYPE_STRING);
			//再读取
			returnValue= cell.getStringCellValue();
			return returnValue;
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
			short format = cell.getCellStyle().getDataFormat();
			System.out.println("format:"+format+";;;;;value:"+cell.getNumericCellValue());
			SimpleDateFormat sdf = null;
			if (format == 14 || format == 31 || format == 57 || format == 58
					|| (176<=format && format<=178) || (182<=format && format<=196)
					|| (210<=format && format<=213) || (208==format ) ) { // 日期
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			} else if (format == 20 || format == 32 || format==183 || (200<=format && format<=209) ) { // 时间
				sdf = new SimpleDateFormat("HH:mm");
			} else { // 不是日期格式
                //强制转换成字符串格再读取
				cell.setCellType(XSSFCell.CELL_TYPE_STRING);
				//再读取
			    returnValue= cell.getStringCellValue();
				return returnValue;
			}
			double value = cell.getNumericCellValue();
			Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
			if(date==null || "".equals(date)){
				return "";
			}
			String result="";
			try {
				result = sdf.format(date);
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
			return result;
		}
		return "";
	}
	
	
	public static String dateToStr(java.util.Date dateDate, String format) {
		// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map buildResultMap(int successFlag,Object dataMap,String errorMessage){
        HashMap resultMap = new LinkedHashMap<String,Object>();
        resultMap.put("status",successFlag);
        resultMap.put("msg",errorMessage);
        resultMap.put("data",dataMap);
        return resultMap;
    }


	public static String fileToBase64(String path) {
		System.out.println("path = " + path);
		String base64 = null;
		InputStream in = null;
		try {
			File file = new File(path);
			in = new FileInputStream(file);
			byte[] bytes = new byte[(int) file.length()];
			in.read(bytes);
			base64 = new String(Base64.encodeBase64(bytes),"UTF-8");
			System.out.println("将文件["+path+"]转base64字符串:"+base64);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return base64;
	}

}
